package com.zhongshengnetwork.rightbe.lang.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.gsonmodel.LangTypeModel;
import com.zhongshengnetwork.rightbe.my.activity.SettingsActivity;

import java.util.ArrayList;
import java.util.List;

public class SelectPublishTypeActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private TopBarView topbar;
    private ListView listview;
    private MyAdapter adapter;
    private List<LangTypeModel> list = new ArrayList<LangTypeModel>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_publish_type);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        listview=(ListView) findViewById(R.id.langtype_listview);
        adapter=new MyAdapter();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("TAG","你点击了"+position);
                if ( list != null && list.size() > 0 && position < list.size()){
                    LangTypeModel langTypeModel = list.get(position);
                    Intent intent = new Intent(SelectPublishTypeActivity.this, LangSearchResultActivity.class);
                    intent.putExtra("type",langTypeModel.getId());
                    intent.putExtra("title",langTypeModel.getName());
                    startActivity(intent);
                }
            }
        });
        LinearLayout select_public_type = (LinearLayout) findViewById(R.id.select_public_type);
        select_public_type.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        listview.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
        listview.setDivider(new ColorDrawable(AppThemeUtils.getInstance().getBackgroundColor()));
        listview.setDividerHeight(ScreenUtils.dip2px(SelectPublishTypeActivity.this, 1));

        initData();
    }

    private void initData(){
        String data = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.LangTypeName,"");
        if (!CommonUtils.isEmpty(data)){
            List<LangTypeModel>l = GsonTools.getLangTypeModel(data);
            if (l != null && l.size() > 0){
                if (list == null){
                    list = new ArrayList<LangTypeModel>();
                }
                list.addAll(l);
                adapter.notifyDataSetChanged();
            }
        }
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            if (convertView == null) {
                convertView = View.inflate(SelectPublishTypeActivity.this, R.layout.notify_listview_item,null);
            }
            View view=convertView;
            TextView title = (TextView) view.findViewById(R.id.notify_listview_item_title);
            TextView time = (TextView) view.findViewById(R.id.notify_listview_item_time);
            LangTypeModel langTypeModel = list.get(position);
            title.setText(langTypeModel.getName());
            time.setText(langTypeModel.getContent());
            title.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            time.setTextColor(AppThemeUtils.getInstance().getLightTextColor());
            RelativeLayout notify_layout = (RelativeLayout) view.findViewById(R.id.notify_layout);
            notify_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            return view;
        }

    }

}
